<!DOCTYPE html>
<html>
	<head>
		<title>经书下载器-目录模式</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<link rel="stylesheet" href="./css/bootstrap.min.css">
		<style>
			body {
				font-family: Arial, sans-serif;
				background-color: #f2f2f2;
				font-size: 14px;
			}

			.form-container {
				max-width: 450px;
				margin: 0 auto;
				padding: 20px;
				background-color: #fff;
				border-radius: 5px;
				box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
			}

			input {
				font-size: 14px;
			}

			h2 {
				text-align: center;
			}

			a {
				margin-left: 40px;
			}

			label {
				display: block;
				margin-bottom: 10px;
			}

			input[type="text"],
			input[type="submit"] {
				width: 100%;
				padding: 8px;
				border: 1px solid #ccc;
				border-radius: 3px;
			}

			select {
				width: 100%;
				padding: 8px;
				border: 1px solid #ccc;
				border-radius: 3px;
			}

			input[type="submit"] {
				background-color: #4CAF50;
				color: #fff;
				cursor: pointer;
			}

			input[type="number"],
			input[type="text"],
			input[type="button"] {
				width: 100%;
				padding: 8px;
				border: 1px solid #ccc;
				border-radius: 3px;
			}

			input[type="button"] {
				background-color: #4CAF50;
				color: #fff;
				cursor: pointer;
			}

			pre {
				font-size: 12px;
			}
		</style>
		<script src="./js/jquery.js"></script>
	</head>
	<body>
		<div class="form-container">
			<div class="h2 text-center"><button class="btn btn-sm btn-default pull-left"
					onclick="uniPage()">菜单</button>经书下载器</div>
			<ul class="nav nav-tabs">
				<li class="active">
					<a id="aindex" href="index.html">目录模式</a>
				</li>
				<li>
					<a id="alist" href="list.html">章节模式</a>
				</li>
			</ul>

			<form id="bookForm">
				<label for="name">书名:</label>
				<input type="text" id="name" name="name" placeholder="输入书名" required><br><br>
				<label for="url"><span class="text-danger">*</span>目录链接:</label>
				<input type="text" id="url" name="url" placeholder="" onblur="check_src(0)" required>
				<span class="help-block">链接中m.推荐换成 www.,打开时有所有章节名的才可用</span>
				<label for="sort">章节顺序:</label>
				<select name="sort" required>
					<option value="0">增加</option>
					<option value="1">减少</option>
				</select>
				<br><br>
				<label for="tags">保留的html标签,分割:</label>
				<input type="text" id="tags" name="tags"
					value="h1,span.title,div#chaptercontent,div#articlecontent,div#content,div.text,p" required>
				<span class="help-block"> 不清楚的话html标签使用默认值，不用修改</span>
				<label for="submenu">目录分页关键词:</label>
				<input type="text" id="submenu" name="submenu" placeholder="">
				<span class="help-block">目录有拆分多页时才需输入，页面显示下一页的文本如：下一页</span>
				<label for="subpage">章节分页关键词:</label>
				<input type="text" id="subpage" name="subpage" placeholder="">
				<span class="help-block">章节有拆分多页时才需输入，页面显示下一页的文本如：下一页</span>
				<label for="prefix">链接关键词:</label>
				<input type="text" id="prefix" name="prefix" placeholder="">
				<span class="help-block">无法解析目录时使用,尝试填入目录链接里最后书的数字id</span>
				<label for="proc">并发数:</label>
				<input type="number" id="proc" name="proc" placeholder="1-30" value=5 required>
				<span class="help-block">同时进行数量1-30</span>
				<label for="start">章节按范围下载(开始-结束，留空下载全部章节):</label>
				<input type="number" style="width: 40%;" id="start" name="start" placeholder="开始序号,从0开始" value="" required>-
				<input type="number" style="width: 40%;" id="end" name="end" placeholder="结束序号,0不限制" value="" required>

				<br></br>
				<input type="button" value="提交" onclick="submitForm()">

			</form>
			<div id="apiCbk"></div>
		</div>
		<div class="alert alert-warning">
			保存根目录或/cyberbook,需要文件权限
		</div>
		<h2 class="text-left"><a id="ahistory" href="history.html">历史记录(<span id="history_num"></span>)</a> <a
				class="pull-right" style="margin-right: 5vw;" href="res.html">资源</a></h2>
		<h2 class="text-left"><a href="praise.html">赞赏</a><a
				href="https://spider.sumroads.site/jsnes/index.html">NES</a></h2>

		<script src="./js/hybrid_html_uni.webview.1.5.5.js"></script>
		<script src="./js/common.js"></script>
		<script>
			var isSubmit = false;
			var his = getHistory()
			var v = GetParam('v');
			document.getElementById("aindex").href += "?v=" + v
			document.getElementById("alist").href += "?v=" + v
			document.getElementById("ahistory").href += "?v=" + v

			if (his) {
				document.getElementById("history_num").innerHTML = his.length
			} else {
				document.getElementById("history_num").innerHTML = 0
			}

			function submitForm() {
				if (isSubmit) {
					return;
				}

				var form = document.getElementById("bookForm");
				var name = form.elements["name"].value;
				var url = form.elements["url"].value;
				var sort = form.elements["sort"].value;
				var tags = form.elements["tags"].value;
				var subpage = form.elements["subpage"].value;
				var submenu = form.elements["submenu"].value;
				var prefix = form.elements["prefix"].value;
				var proc = form.elements["proc"].value;
				var start = form.elements["start"].value;
				var end = form.elements["end"].value;
				if (!name) {
					alert("请输入任意书名")
					return
				}
				isSubmit = true;
				document.getElementById("apiCbk").innerHTML = "加载中:";
				var xhr = new XMLHttpRequest();
				xhr.open("POST", "/api/bookMenuDownload", true);
				xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xhr.onreadystatechange = function() {
					if (xhr.readyState === 4 && xhr.status === 200) {
						// alert("Form submitted successfully!");
						var res = JSON.parse(xhr.responseText)
						if (res.Code == 0) {
							console.log(xhr.responseText)
							if (res.Data.file_path) {
							
								res.Data.file_path = res.Data.file_path.replace(/\\\\/g, '/');
								res.Data.file_path = res.Data.file_path.replace(/\\/g, '/');
								console.log(res.Data.file_path);
								var tpl = res.Data.root_path + ' | ' + res.Data.file_path +
									`<br/><a class="btn btn-link" href="` + res.Data.file_path + `" download="` + name +
									`.txt" target="_blank" onclick="unidown('` + res.Data.file_path + `','` + name +
									`.txt')" >点此保存</a>`;
								if (v != "") {
									tpl = tpl.replace(/<a/g, '<span');
									tpl = tpl.replace(/<\/a/g, '</span');
								}
							} else {
								tpl = res.Msg;
							}
							document.getElementById("apiCbk").innerHTML = tpl

						} else {
							document.getElementById("apiCbk").innerHTML = res.Msg;
						}
						isSubmit = false;
					} else if (xhr.readyState === 4) {
						alert("Error: " + xhr.responseText);
						isSubmit = false;
					}
				};
				//JSON.stringify()
				var data = {
					"name": name,
					"url": url,
					"sort": sort,
					"tags": tags,
					"subpage": subpage,
					"submenu": submenu,
					"proc": proc,
					"prefix": prefix,
					"start": start,
					"end": end
				};
				xhr.send(objectToFormUrlEncoded(data));
				var history = getHistory()
				history.push(data)
				saveHistory(history)
				document.getElementById("history_num").innerHTML = history.length
			}

			function unidown(url, name) {
				uni.postMessage({
					data: {
						name: name,
						url: url,
					}
				})
			}
		</script>
	</body>
</html>